package com.abc.robot01.activity.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import com.abc.robot01.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class DrawerDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
            val drawerOffset = drawerState.currentOffset
            val scope = rememberCoroutineScope()
            Surface(color = Color.Blue, modifier = Modifier.fillMaxSize()) {
                Row {
                    MyDrawerExample(drawerState, drawerOffset, scope)
                    if (drawerState.isClosed) {
                        Image(
                            modifier = Modifier.offset(((drawerOffset) / 3.5f).dp, 0.dp)
                                .padding(top = (30f / 3.5f).dp),
                            painter = painterResource(id = R.drawable.open),
                            contentDescription = "Your Image",
                            contentScale = ContentScale.Crop
                        )
                    }
                }
            }
        }
    }
}

    @Composable
    fun LazyColumn01() {
        LazyColumn(
        ) {
            item {
                Text(text = "First item")
            }
            // Add 5 items
            items(500) { index ->
                Text(text = "Item: $index")
            }
            // Add another single item
            item {
                Text(text = "Last item")
            }
        }
    }

    @Composable
    fun MyDrawerExample(drawerState: DrawerState,drawerOffset:Float,scope : CoroutineScope) {
        ModalNavigationDrawer(
            drawerState = drawerState,
            modifier = Modifier
                .width(200.dp)
                .fillMaxHeight(),
            scrimColor = Color(0x00667788),
            drawerContent = {
                ModalDrawerSheet(drawerContainerColor=Color(0x00000000), modifier = Modifier.fillMaxSize()) {
                    ConstraintLayout(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(Color.LightGray)
                    ) {
                        val (button, close) = createRefs()
                        Image(
                            painter = painterResource(id = R.drawable.close),
                            contentDescription = "Your Image",
                            contentScale = ContentScale.Crop,
                            modifier = Modifier.constrainAs(close) {
                                top.linkTo(parent.top)
                                end.linkTo(parent.end)
                                bottom.linkTo(parent.bottom)
                            }
                        )
                    }
                }
            },
            content = {
                Column {
//                    Button(
//                        modifier = Modifier.offset((20).dp,0.dp)
//                        ,onClick = {
//                        scope.launch {
//                            if(drawerState.isOpen) drawerState.close()
//                            else
//                            drawerState.open()
//                        }
//                    }) {
//                        Text(text = "Open")
//                    }
                    LazyColumn01()
                }
            }
        )
    }


